﻿using FangChanCrm.Services.Interface;
using FangChanCrmEF;
using Maticsoft.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FangChanCrm.Controllers
{
    public class ZhanDuiController : Controller
    {
        private readonly It_ZhanDuiBiaoService t_ZhanDuiBiaoService;
        private readonly It_RenYuanBiaoService t_RenYuanBiaoService;
        private readonly It_ZhanQuBiaoService t_ZhanQuBiaoService;

        public ZhanDuiController(It_ZhanDuiBiaoService zhanDuiBiaoService, It_RenYuanBiaoService renYuanBiaoService, It_ZhanQuBiaoService zhanQuBiaoService, IConfiguration configuration)
        {
            t_ZhanDuiBiaoService = zhanDuiBiaoService;
            t_RenYuanBiaoService = renYuanBiaoService;
            t_ZhanQuBiaoService = zhanQuBiaoService;
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }



        public IActionResult ZhanDuiList()
        {
            if (HttpContext.Session.GetString("loginid") == null)
            {
                HttpContext.Request.Cookies.TryGetValue("账户", out string value);
                if (value == null || value == "")
                {
                    return View("../Login/Login");
                }
                else
                {
                    return View("../Login/LoginChongXin");
                }
            }
            else
            {
                return View();
            }
        }

        public IActionResult ZhanDuiAdd()
        {
            if (HttpContext.Session.GetString("loginid") == null)
            {
                HttpContext.Request.Cookies.TryGetValue("账户", out string value);
                if (value == null || value == "")
                {
                    return View("../Login/Login");
                }
                else
                {
                    return View("../Login/LoginChongXin");
                }
            }
            else
            {
                return View();
            }
        }

        public IActionResult ZhanDuiUpdate()
        {
            if (HttpContext.Session.GetString("loginid") == null)
            {
                HttpContext.Request.Cookies.TryGetValue("账户", out string value);
                if (value == null || value == "")
                {
                    return View("../Login/Login");
                }
                else
                {
                    return View("../Login/LoginChongXin");
                }
            }
            else
            {
                return View();
            }
        }

        public string Getid()
        {
            string id = Request.Form["id"];
            t_ZhanDuiBiao t_ZhanDuiBiao = t_ZhanDuiBiaoService.GetId(Convert.ToInt32(id));
            string shuju = "{\"ZhanDuiMingCheng\":\"" + t_ZhanDuiBiao.ZhanDuiMingCheng + "\",\"ZhanQuId\":\"" + t_ZhanDuiBiao.ZhanQuId + "\"}";
            return shuju;
        }

        public string Update()
        {
            string id = Request.Form["id"];
            string zhanduimingcheng = Request.Form["zhanduimingcheng"];
            string suoshuzhanqu = Request.Form["suoshuzhanqu"];

            bool panduan = false;
            var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
            optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
            using (var _context = new MyContext(optionsBuilder.Options))
            {
                var zhandui = _context.t_ZhanDuiBiao.FirstOrDefault(x => x.id == Convert.ToInt32(id));
                zhandui.ZhanDuiMingCheng = zhanduimingcheng;
                zhandui.ZhanQuId = Convert.ToInt32(suoshuzhanqu);
                panduan = _context.SaveChanges() > 0;
            }

            //t_ZhanDuiBiao zhandui = t_ZhanDuiBiaoService.GetId(Convert.ToInt32(id));
            //zhandui.ZhanDuiMingCheng = zhanduimingcheng;
            //zhandui.ZhanQuId = Convert.ToInt32(suoshuzhanqu);

            //bool panduan = t_ZhanDuiBiaoService.Update(zhandui);

            if (panduan)
            {
                return "1";
            }
            else
            {
                return "0";
            }
        }

        public string QiYong()
        {
            string id = Request.Form["id"];
            bool panduan = false;
            var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
            optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
            using (var _context = new MyContext(optionsBuilder.Options))
            {
                var zhandui = _context.t_ZhanDuiBiao.FirstOrDefault(x => x.id == Convert.ToInt32(id));
                zhandui.ZhuangTai = "启用";
                panduan = _context.SaveChanges() > 0;
            }

            //t_ZhanDuiBiao t_ZhanDuiBiao = t_ZhanDuiBiaoService.GetId(Convert.ToInt32(id));
            //t_ZhanDuiBiao.ZhuangTai = "启用";
            //bool panduan = t_ZhanDuiBiaoService.Update(t_ZhanDuiBiao);
            if (panduan)
            {
                return "success";
            }
            else
            {
                return "err";
            }
        }

        public string JinYong()
        {
            string id = Request.Form["id"];

            bool panduan = false;
            var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
            optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
            using (var _context = new MyContext(optionsBuilder.Options))
            {
                var zhandui = _context.t_ZhanDuiBiao.FirstOrDefault(x => x.id == Convert.ToInt32(id));
                zhandui.ZhuangTai = "禁用";
                panduan = _context.SaveChanges() > 0;
            }

            //t_ZhanDuiBiao t_ZhanDuiBiao = t_ZhanDuiBiaoService.GetId(Convert.ToInt32(id));
            //t_ZhanDuiBiao.ZhuangTai = "禁用";
            //bool panduan = t_ZhanDuiBiaoService.Update(t_ZhanDuiBiao);
            if (panduan)
            {
                return "success";
            }
            else
            {
                return "err";
            }
        }

        public string GetZhanQu()
        {
            StringBuilder Json = new StringBuilder();
            List<t_ZhanQuBiao> bumen = t_ZhanDuiBiaoService.GetZhanQuList();
            Json.Append("[");
            for (int i = 0; i < bumen.Count; i++)
            {
                t_ZhanQuBiao bum = bumen[i];
                Json.Append("{\"id\":\"" + bum.Id + "\",\"zhanQuMingCheng\":\"" + bum.ZhanQuMingCheng + "\"}");
                if ((i + 1) == bumen.Count)
                {

                }
                else
                {
                    Json.Append(",");
                }
            }
            Json.Append("]");
            return Json.ToString();
        }

        public string GetRenYuan()
        {
            StringBuilder Json = new StringBuilder();
            List<t_RenYuanBiao> renyuan = t_RenYuanBiaoService.GetList();
            Json.Append("[");
            for (int i = 0; i < renyuan.Count; i++)
            {
                t_RenYuanBiao ry = renyuan[i];
                Json.Append("{\"id\":\"" + ry.Id + "\",\"xingMing\":\"" + ry.XingMing + "\",\"lianXiFangShi\":\"" + ry.LianXiFangShi + "\"}");
                if ((i + 1) == renyuan.Count)
                {

                }
                else
                {
                    Json.Append(",");
                }
            }
            Json.Append("]");
            return Json.ToString(); ;
        }

        public string ZhanDuiTianJia()
        {
            string zhanduimingcheng = Request.Form["zhanduimingcheng"];
            string suoshuzhanqu = Request.Form["suoshuzhanqu"];

            t_ZhanDuiBiao zhandui = new t_ZhanDuiBiao();
            zhandui.ZhanDuiMingCheng = zhanduimingcheng;
            zhandui.ZhanQuId = Convert.ToInt32(suoshuzhanqu);
            zhandui.ZhuangTai = "启用";

            int panduan = 0;

            panduan = t_ZhanDuiBiaoService.Add(zhandui);

            if (panduan > 0)
            {
                panduan = 1;
            }
            else
            {
                panduan = 0;
            }
            return panduan.ToString();
        }

        public string InitializationDataTable()
        {
            t_ZhanDuiBiao t_ZhanDuiBiao = new t_ZhanDuiBiao();
            t_ZhanDuiBiao.ZhanDuiMingCheng = Request.Form["ipt_zhanduimingcheng"];
                t_ZhanDuiBiao.ZhanQuId = Convert.ToInt32(Request.Form["slt_zhanduisuoshuzhanqu"]);
            t_ZhanDuiBiao.ZhuangTai = Request.Form["slt_zhanduizhuangtai"];
            return t_ZhanDuiBiaoService.GetListWithFenYe(t_ZhanDuiBiao, Convert.ToInt32(Request.Form["start"]), Convert.ToInt32(Request.Form["limit"]), Request.Form["dir"], Convert.ToInt32(Request.Form["column"]));
        }

    }
}
